/**
 * 敌人管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 敌人的难度等级
         */
        typeNumber: {
            default: 0,
            tooltip: "敌人的难度等级数量"
        },
        /**
         * 每难度层级的数量
         */
        numberInPerDifficulty: {
            default: 0,
            tooltip: "每难度层级的数量"
        },
    },

    onLoad () {
        //存放所有敌人预设的二维数组
        this._enemy = new Array();
        //上一次取得难度等级的下标
        this._lastEnemyIndex = new Array();
        this.loadEnemyPrefab();
    },

    start () {

    },

    // update (dt) {},

    /**
     * 加载敌人预设
     */
    loadEnemyPrefab () {
        var self = this;
        for (var i = 0; i < this.typeNumber; i++) {
            self._enemy[i] = new Array();
            self._lastEnemyIndex[i] = -1;
            cc.loader.loadResDir('enemy/' + i.toString(), function (err, assets, urls) {
                if (err) {
                    cc.error(err);
                    return;
                }
                //手动读取加载时的文件夹名字
                if (urls.length > 0){
                    var index = parseInt(urls[0].split('/')[1]);
                    self._enemy[index] = assets;
                }
            });
        }
    },

    /**
     * 根据难度等级取敌人预制体
     * @param {number} difficulty 难度等级
     * @returns 敌人预制体
     */
    getEnemyPrefab (difficulty) {
        if (difficulty >= this.typeNumber) return;
        if (this._enemy[difficulty].length == 0) return null;
        var self = this;
        //与上一次取的同难度等级的实体不同
        var index = 0;
        if (self._lastEnemyIndex[difficulty] == -1){
            index = Math.floor(cc.random0To1()*self._enemy[difficulty].length);
        }
        else {
            //取下一个
            index = (self._lastEnemyIndex[difficulty] + 1) % self._enemy[difficulty].length;
        }
        self._lastEnemyIndex[difficulty] = index;
        return self._enemy[difficulty][index];
    } 
});
